import { constantRouterMap } from "@/basa/router";
// import store from "../index";
// import Layout from '@/views/layout/Layout'
const _PERMISSION = {
  state: {
    routers: constantRouterMap,
    addRouters: []
  },
  mutations: {
    setRouters: (state, routers) => {
      state.addRouters = routers;
      // 和已经存在的路由表拼接
      state.routers = constantRouterMap.concat(routers);
      state.routers.push({ path: "*", redirect: "/" });
      // debugger;
      // console.log(routers[0].path);
      // state.addRouters.push({
      //   path: "/appActivation",
      //   redirect: routers[0].path
      // });
    }
  },
  getters: {
    addRouters: state => {
      return state.addRouters;
    }
  },
  actions: {
    generateRoutes({ commit }, response) {
      // 路由处理`
      let asyncRouters = filterAsyncRouter(response);
      // todo
      asyncRouters.push({ path: "*", redirect: "/404", hidden: true });

      const routers = [];
      asyncRouters.forEach(item => {
        if (item.children && item.children.length) {
          // item.children = item.children[0].children;
          let arr = [];
          item.children.forEach(itemSub => {
            arr = arr.concat(itemSub.children);
          });
          item.children = arr;
        }

        routers.push(item);
      });
      commit("setRouters", routers);
    }
  }
};

function filterAsyncRouter(routers) {
  // 遍历后台传来的路由字符串，转换为组件对象
  let accessedRouters = routers.filter(router => {
    router.path = router.url;
    router.meta = {
      // 由于接口name 于路由name冲突 需把meta放上面
      title: router.name,
      hasStatistics: false,
      keepAlive: true,
      isShowSidebar: !!router.showLeftMenu
    };
    router.name = router.keyword;
    if (
      router.type === 2 ||
      router.viewTemplate === "common-modules/layout/Layout" ||
      router.viewTemplate === "common-modules/parent-view/index"
    ) {
      router.component = loadView(router.viewTemplate);
    }

    if (router.sons && router.sons.length) {
      if (router.type === 1) {
        // 如果展现左侧 并且类型是菜单 重定向到非菜单属性的第一条数据的path
        router.redirect = getRedirect(router.sons);
      }

      // debugger;
      router.children = filterAsyncRouter(router.sons);
    }

    delete router.auth;
    delete router.auths;
    delete router.code;
    // delete router.id;
    delete router.isGroup;
    delete router.keyword;
    delete router.pcode;
    delete router.showLeftMenu;
    delete router.sons;
    delete router.url;
    delete router.viewTemplate;
    delete router.type;

    // 存在子集
    /* if (router.children && router.children.length) {
      router.children = filterAsyncRouter(router.children)
    } */
    return true;
  });
  return accessedRouters;
}
function loadView(view) {
  // 路由懒加载
  return () => import(`@/app/${view}`);
}

function getRedirect(sons) {
  // 重定向过滤
  if (sons && sons[0].type === 2 && sons[0].sons && sons[0].sons.length === 0) {
    return sons[0].url;
  } else {
    return getRedirect(sons[0].sons);
  }
}

export default _PERMISSION;
